import http from '../../util/request.js';
import alert from '../../util/alert.js';
var app = getApp();

Page({
  data: {
    member_sex: '1',
    member_mobile:'',
    member_email:'',
    member_qq:'',
    member_nickname: '',
    member_birthday:'',
    member_addr: '',

    modalHidden:true,
  },

  onLoad: function (options) {
    var that = this;
    var member = wx.getStorageSync('memberInfo');
    if(!member){
      http.get('api', { r: 'user.getMemberInfo' }).then(data => {
        that.setData({
          member_sex: data.member_sex,
          member_mobile: data.member_mobile,
          member_email: data.member_email,
          member_qq: data.member_qq,
          member_nickname: data.member_nickname,
          member_birthday: data.member_birthday,
          member_addr: data.member_addr
        });
      });
    }else{
      that.setData({
        member_sex: member.member_sex,
        member_mobile: member.member_mobile,
        member_email: member.member_email,
        member_qq: member.member_qq,
        member_nickname: member.member_nickname,
        member_birthday: member.member_birthday,
        member_addr: member.member_addr
      });
    }

    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: app.globalData.XcxThemeColor
    });
  },

  member_sex: function (e) {
    var that = this;
    that.setData({
      member_sex: e.detail.value
    })
  },

  member_mobile: function (e) {
    var that = this;
    that.setData({
      member_mobile: e.detail.value
    })
  },

  
  member_email: function (e) {
    var that = this;
    that.setData({
      member_email: e.detail.value
    })
  },

  member_qq: function (e) {
    var that = this;
    that.setData({
      member_qq: e.detail.value
    })
  },
  member_birthday:function(e){
    var that = this;
    that.setData({
      member_birthday: e.detail.value
    })
  },
  getaddr:function(e){//定位
    wx.chooseLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res);
        that.setData({
          member_addr: res.address
        })
      }
    })
  },
  bindRegionChange: function (e) {
    var that = this;
    that.setData({
      member_addr: e.detail.value
    })
  },
  setaddr: function (e) {
    var that = this;
    that.setData({
      member_addr: e.detail.value
    })
  },
  //显示
  modalTap:function(e){
     var that = this;
     that.setData({
        modalHidden:false
     })
  },
  
  //隐藏并保存
  modalChange: function (e) { 
    var that = this;
    that.modifyMemberInfo();
    that.setData({
      modalHidden: true
    })
  },
  
  //隐藏
  modalCancel: function (e) {
    var that = this;
    that.setData({
      modalHidden: true
    })
  },

  modifyMemberInfo:function(){
     var that = this;

     var wxinfo=wx.getStorageSync('wxInfo');
     var params = {
       r: 'user.saveMemberInfo',
        member_sex: that.data.member_sex,
        member_mobile: that.data.member_mobile,
        member_email: that.data.member_email,
        member_qq: that.data.member_qq,
        member_nickname: wxinfo.nickName,
        member_birthday: that.data.member_birthday,
        member_addr: that.data.member_addr
     }
     //console.log(params);
     http.get('api', params).then(data => {
      if(!data.errno){
        wx.setStorageSync('memberInfo', params);
        alert('保存成功');
      }else{
        //console.log(data);
      }

     });
  } 
})